﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;

public partial class Common_ChoiceOrgPers_List : System.Web.UI.Page
{
    public string sHtml = "<tr>";
    protected string sType = "1";     //1是查询，2是左边树传值
    protected string sPersName = "";  //人员姓名
    protected string sPersCode = "";  //员工号
    protected string sOrgID = "";     //部门ID
    protected string sOrgName = "";  //部门名称

    BaiMaSoft .Conn.DataConn MyDataConn = new BaiMaSoft .Conn.DataConn();
    public SqlConnection myconn;
    public SqlCommand mycomm;


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            try
            {
                sType = Request.QueryString["OpType"].ToString().Trim();
            }
            catch
            {
                sType = "1";
            }

            try
            {
                sPersName = Request.QueryString["PersName"].ToString().Trim();
            }
            catch
            {
                sPersName = "";
            }

            try
            {
                sPersCode = Request.QueryString["PersCode"].ToString().Trim();
            }
            catch
            {
                sPersCode = "";
            }

            try
            {
                sOrgID = Request.QueryString["OrgID"].ToString().Trim();
            }
            catch
            {
                sOrgID = "";
            }

            try
            {
                sOrgName = Request.QueryString["OrgName"].ToString().Trim();
            }
            catch
            {
                sOrgName = "";
            }            

            if (sPersName.Trim() != "" || sPersCode.Trim() != "" || sOrgID.Trim() != "")
            {
                if (sOrgID.Trim() != "" && sOrgName.Trim() != "")
                {
                    OrgCount.Value = "1";
                    OrgName.Text = "&nbsp;&nbsp; <input type='checkbox' id='OrgCK' name='OrgCK'  onclick='ChoiceOrg(this);'  value='" + sOrgID.Trim() + "," + sOrgName.Trim() + "' >" + sOrgName.Trim() + "&nbsp;&nbsp;<input type='checkbox' id='AllPerCK' name='AllPerCK'  onclick='ChoiceAllPer(this);' >全选 ";
                }
                else
                {
                    OrgCount.Value = "0";
                    OrgName.Text = "&nbsp;&nbsp; <input type='checkbox' id='AllPerCK' name='AllPerCK'  onclick='ChoiceAllPer(this);' >全选 ";  // 
                }

                IniInfo(sPersName, sPersCode, sOrgID);
            }
        }
    }

    public void IniInfo(string PersName, string PersCode, string OrgID)
    {
        MyDataConn.Connection.Open();
        string proc = "  SELECT a.DeptID AS OrgID, b.Name AS PersName, b.PersCode, b.ID AS PersID " +
                      "  FROM Sys_DeptPers_Info a LEFT OUTER JOIN " +
                      "        Sys_Pers_Info b ON a.PersID = b.ID " +
                      "  WHERE (a.Effective = '1') AND (b.Effective = '1') ";
        if (PersName.Trim() != "")
        {
            proc += " and b.Name  like '%" + PersName.Trim() + "%' ";
        }

        if (PersCode.Trim() != "")
        {
            proc += " and  b.PersCode  like '%" + PersCode.Trim() + "%' ";
        }

        if (OrgID.Trim() != "")
        {
            proc += " and  a.DeptID  in(" + OrgID.Trim() + ") ";
        }

        proc += "  ORDER BY a.DPOrder, b.PersCode ";
        mycomm = new SqlCommand(proc, MyDataConn.Connection);
        SqlDataReader ShowReader = mycomm.ExecuteReader();
        int iPersCount = 0; //人员数
        while (ShowReader.Read())
        {
            string sOrgID = ShowReader["OrgID"].ToString().Trim();      // 组织ID
            string sPersName = ShowReader["PersName"].ToString().Trim();   // 人员名称
            string sPersCode = ShowReader["PersCode"].ToString().Trim();   // 员工号
            string sPersID = ShowReader["PersID"].ToString().Trim();     // 人员ID

            string ChioceBox = "<TD> <input type=\"checkbox\" name=\"PersCK\" id=\"PersCK\"  onclick=\"ChoicePer(this);\" value=\"" + sPersID + "," + sPersName + "\" />" + sPersName + "(" + sPersCode + ") </td>";

            if (iPersCount % 3 == 0)   //一行显示3个员工姓名
            {
                sHtml += " </tr><tr> " + ChioceBox;
            }
            else
            {
                sHtml += ChioceBox;
            }
            iPersCount += 1;
        }
        PersCount.Value = Convert.ToString(iPersCount);
        ShowReader.Close();
        MyDataConn.Connection.Close();
        sHtml += "</tr>";
    }
}
